package com.avecsys.quotemanager.service.user;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.avecsys.quotemanager.constants.BusinessCodes;
import com.avecsys.quotemanager.dao.common.EnumDBRecordStatus;
import com.avecsys.quotemanager.dao.common.EnumTransactionStatus;
import com.avecsys.quotemanager.dao.user.UserMasterDAO;
import com.avecsys.quotemanager.exception.BusinessException;
import com.avecsys.quotemanager.exception.DAOException;
import com.avecsys.quotemanager.pojos.BuyerUsersPojo;
import com.avecsys.quotemanager.pojos.SupplierUsersPojo;
import com.avecsys.quotemanager.pojos.SystemUsersPojo;
import com.avecsys.quotemanager.pojos.UserMasterPojo;
import com.avecsys.quotemanager.service.security.CryptoService;
import com.avecsys.quotemanager.utils.TransactionStatus;

public class UserMasterServiceImpl implements UserMasterService {

	private static final Log log = LogFactory
			.getLog(UserMasterServiceImpl.class);

	private UserMasterDAO userMasterDAO;
	
	private CryptoService cryptoService;

	/**
	 * @return the userMasterDAO
	 */
	public UserMasterDAO getUserMasterDAO() {
		return userMasterDAO;
	}

	public CryptoService getCryptoService() {
		return cryptoService;
	}

	public void setCryptoService(CryptoService cryptoService) {
		this.cryptoService = cryptoService;
	}

	/**
	 * @param userMasterDAO
	 *            the userMasterDAO to set
	 */
	public void setUserMasterDAO(UserMasterDAO userMasterDAO) {
		this.userMasterDAO = userMasterDAO;
	}

	/**
	 * Method to save the supplier User Information
	 * 
	 * @param supplierUsersPojo
	 * @return TransactionStatus
	 * @throws BusinessException
	 */
	@Override
	public TransactionStatus saveSupplierUser(
			SupplierUsersPojo supplierUsersPojo) throws BusinessException {
		TransactionStatus transactionStatus = new TransactionStatus(
				EnumTransactionStatus.SAVE_FAIL.ordinal());
		try {
			// Step1:
			// if the Supplier Master information already persisted then update
			// other wise create a new supplier information with default use
			// Mark As Active
			supplierUsersPojo.setStatus(EnumDBRecordStatus.ACTIVE.status());
			supplierUsersPojo.getUserMasterPojo().setStatus(
					EnumDBRecordStatus.ACTIVE.status());
			supplierUsersPojo.getUserMasterPojo().setUserType(
					BusinessCodes.USER_TYPE_SUPPLIER);
			//Hashing of password
			supplierUsersPojo.getUserMasterPojo().setPassword(cryptoService.encryptPassword(supplierUsersPojo.getUserMasterPojo().getPassword()));
			if (supplierUsersPojo.isPersisted()) {
				transactionStatus.setStatus(EnumTransactionStatus.UPDATE_FAIL
						.ordinal());
				transactionStatus = userMasterDAO
						.updateSupplierUser(supplierUsersPojo);
			} else {
				transactionStatus = userMasterDAO
						.saveSupplierUser(supplierUsersPojo);
			}
		} catch (DAOException e) {
			log.error(e);
			throw new BusinessException(e);
		}
		return transactionStatus;
	}
	
	
	/**
	 * Method to save the Buyer User Information
	 * 
	 * @param buyerUsersPojo
	 * @return TransactionStatus
	 * @throws BusinessException
	 */
	@Override
	public TransactionStatus saveBuyerUser(
			BuyerUsersPojo buyerUsersPojo) throws BusinessException {
		TransactionStatus transactionStatus = new TransactionStatus(
				EnumTransactionStatus.SAVE_FAIL.ordinal());
		try {
			// Step1:
			// if the Buyer Master information already persisted then update
			// other wise create a new buyer user information with default use
			// Mark As Active
			buyerUsersPojo.setStatus(EnumDBRecordStatus.ACTIVE.status());
			buyerUsersPojo.getUserMasterPojo().setStatus(
					EnumDBRecordStatus.ACTIVE.status());
			buyerUsersPojo.getUserMasterPojo().setUserType(
					BusinessCodes.USER_TYPE_BUYER);
			//Hashing of password
			buyerUsersPojo.getUserMasterPojo().setPassword(cryptoService.encryptPassword(buyerUsersPojo.getUserMasterPojo().getPassword()));
			if (buyerUsersPojo.isPersisted()) {
				transactionStatus.setStatus(EnumTransactionStatus.UPDATE_FAIL
						.ordinal());
				transactionStatus = userMasterDAO
						.updateBuyerUser(buyerUsersPojo);
			} else {
				transactionStatus = userMasterDAO
						.saveBuyerUser(buyerUsersPojo);
			}
		} catch (DAOException e) {
			log.error(e);
			throw new BusinessException(e);
		}
		return transactionStatus;
	}

	/**
	 * Method to return the userMasterPojo instance by id
	 * 
	 * @param userId
	 * @return UserMasterPojo
	 * @throws BusinessException
	 */
	@Override
	public UserMasterPojo getUserMasterById(Long userId)
			throws BusinessException {
		UserMasterPojo userMasterPojo = new UserMasterPojo();
		try {
			userMasterPojo = (UserMasterPojo) userMasterDAO.findActiveById(
					userMasterPojo, userId);
		} catch (DAOException e) {
			throw new BusinessException(e);
		}
		return userMasterPojo;
	}

	@Override
	public TransactionStatus deleteSupplierUser(
			SupplierUsersPojo supplierUsersPojo) throws BusinessException {
		TransactionStatus transactionStatus = new TransactionStatus(
				EnumTransactionStatus.DELETE_FAIL.ordinal());
		try {
			transactionStatus = userMasterDAO
					.deleteSupplierUser(supplierUsersPojo);
		} catch (DAOException e) {
			throw new BusinessException(e);
		}
		return transactionStatus;
	}

	/**
	 * Method to return the SupplierMasterPojo instance by id
	 * 
	 * @param parseLong
	 * @return SupplierUsersPojo
	 * @throws BusinessException
	 */
	@Override
	public SupplierUsersPojo getSupplierUserById(long supplierUserId)
			throws BusinessException {
		SupplierUsersPojo supplierUsersPojo = new SupplierUsersPojo();
		try {
			supplierUsersPojo = (SupplierUsersPojo) userMasterDAO
					.findActiveById(supplierUsersPojo, supplierUserId);
		} catch (DAOException e) {
			throw new BusinessException(e);
		}
		return supplierUsersPojo;
	}
	
	
	@Override
	public TransactionStatus deleteBuyerUser(
			BuyerUsersPojo buyerUsersPojo) throws BusinessException {
		TransactionStatus transactionStatus = new TransactionStatus(
				EnumTransactionStatus.DELETE_FAIL.ordinal());
		try {
			transactionStatus = userMasterDAO
					.deleteBuyerUser(buyerUsersPojo);
		} catch (DAOException e) {
			throw new BusinessException(e);
		}
		return transactionStatus;
	}

	/**
	 * Method to return the SupplierMasterPojo instance by id
	 * 
	 * @param parseLong
	 * @return SupplierUsersPojo
	 * @throws BusinessException
	 */
	@Override
	public BuyerUsersPojo getBuyerUserById(long buyerUserId)
			throws BusinessException {
		BuyerUsersPojo buyerUsersPojo = new BuyerUsersPojo();
		try {
			buyerUsersPojo = (BuyerUsersPojo) userMasterDAO
					.findActiveById(buyerUsersPojo, buyerUserId);
		} catch (DAOException e) {
			throw new BusinessException(e);
		}
		return buyerUsersPojo;
	}

	/**
	 * Method to save the system User Information
	 * 
	 * @param systemUsersPojo
	 * @return TransactionStatus
	 * @throws BusinessException
	 */
	@Override
	public TransactionStatus saveSystemUser(SystemUsersPojo systemUsersPojo)
			throws BusinessException {
		TransactionStatus transactionStatus = new TransactionStatus(
				EnumTransactionStatus.SAVE_FAIL.ordinal());
		try {
			// Step1:
			// if the Supplier Master information already persisted then update
			// other wise create a new supplier information with default use
			// Mark As Active
			systemUsersPojo.setStatus(EnumDBRecordStatus.ACTIVE.status());
			systemUsersPojo.getUserMasterPojo().setStatus(EnumDBRecordStatus.ACTIVE.status());
			systemUsersPojo.getUserMasterPojo().setUserType(BusinessCodes.USER_TYPE_SYSTEM_ADMIN);
			//Hashing of password
			systemUsersPojo.getUserMasterPojo().setPassword(cryptoService.encryptPassword(systemUsersPojo.getUserMasterPojo().getPassword()));
			if (systemUsersPojo.isPersisted()) {
				transactionStatus.setStatus(EnumTransactionStatus.UPDATE_FAIL
						.ordinal());
				transactionStatus = userMasterDAO
						.updateSystemUser(systemUsersPojo);
			} else {
				transactionStatus = userMasterDAO
						.saveSystemUser(systemUsersPojo);
			}
		} catch (DAOException e) {
			log.error(e);
			throw new BusinessException(e);
		}
		return transactionStatus;
	}

	/**
	 * Method to mark the system user information as deleted
	 * 
	 * @param systemUsersPojo
	 * @return TransactionStatus
	 * @throws BusinessException
	 */
	@Override
	public TransactionStatus deleteSystemUser(SystemUsersPojo systemUsersPojo)
			throws BusinessException {
		TransactionStatus transactionStatus = new TransactionStatus(
				EnumTransactionStatus.DELETE_FAIL.ordinal());
		try {
			transactionStatus = userMasterDAO
					.deleteSystemUser(systemUsersPojo);
		} catch (DAOException e) {
			throw new BusinessException(e);
		}
		return transactionStatus;
	}

	/**
	 * Method to return the SystemUserPojo instance by id
	 * 
	 * @param parseLong
	 * @return SystemUserPojo
	 * @throws BusinessException
	 */
	@Override
	public SystemUsersPojo getSystemUserById(long systemUserId)
			throws BusinessException {
		SystemUsersPojo systemUsersPojo = new SystemUsersPojo();
		try {
			systemUsersPojo = (SystemUsersPojo) userMasterDAO
					.findActiveById(systemUsersPojo, systemUserId);
		} catch (DAOException e) {
			throw new BusinessException(e);
		}
		return systemUsersPojo;
	}
}
